package com.exam.service.impl;

import com.exam.mapper.ScoreMapper;
import com.exam.pojo.*;
import com.exam.service.ClassesService;
import com.exam.service.ScoreService;
import com.exam.util.GradeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 黄星
 * 分数服务实现类
 */
@Service("scoreServiceImpl")
public class ScoreServiceImpl implements ScoreService {

	@Resource(name = "scoreMapper")
	private ScoreMapper scoreMapper;
	@Resource(name="classesServiceImpl")
	private ClassesService classesService;
	@Resource(name="paging")
	Paging<Score> paging;


	public List<Score> getScoreBySno(String sno) {
		return scoreMapper.getScoreBySno( sno );
	}

	public Integer insScore(String sno, Integer subjectId, Integer result) {
		Boolean ins = true;
		int ret = 0;
		List<Score> scores = getScoreBySno( sno );
		for(int i = 0 ; i < scores.size();i++){
			if(scores.get( i ).getSubjectId() == subjectId){
				ins = false;
				break;
			}
		}
		if(ins){
			Map<String,Object> map = new HashMap<String, Object>(  );
			map.put( "sno",sno );
			map.put( "subjectId",subjectId );
			map.put( "result",result );
			ret = scoreMapper.insScore( map );
		}

		return ret;
	}

	public Paging<Score> scorePaging(Map map) {
		Map<String,Object> param = new HashMap<String, Object>(  ); //参数列表
		Map<String, Object> selCountMap = new HashMap<String, Object>();
		Integer pageSize = (Integer) map.get( "pageSize" );
		Integer page = (Integer) map.get( "page" );
		Integer totalCount = 0;

		Integer majorId = (Integer) map.get( "majorId" );
		Integer classesId =  (Integer)map.get( "classesId" ) ;
		Integer sno = (Integer) map.get( "sno" );
		Integer subjectId = (Integer) map.get( "subjectId" );

		if(subjectId > 0){
			selCountMap.put( "subjectId", subjectId);
			param.put( "subjectId", subjectId);
		}else{
			if(majorId > 0){
				selCountMap.put( "majorId",majorId );
				param.put( "majorId",majorId );
			}

			if(classesId > 0  ){
					selCountMap.put( "classesId",classesId );
					param.put( "classesId",classesId );
			}
		}

		if(sno > 0){
			selCountMap.put( "sno", sno);
			param.put( "sno", sno);
		}
		//填充参数列表
		param.put( "start", (page - 1) * pageSize );
		param.put( "offset", pageSize );

		totalCount = scoreMapper.getTotalCount( selCountMap );
		List<Score> scores = scoreMapper.scorePaging( param );
		paging.setTotalCount( totalCount ); //设置总记录数
		paging.setCurrentPage( page ); //设置当前页
		paging.setPageSize( pageSize ); //设置页面大小
		paging.setList( scores );

		if (totalCount % pageSize == 0) {
			paging.setTotalPage( totalCount / pageSize );
		} else {
			paging.setTotalPage( (totalCount / pageSize) + 1 );
		}
		return paging;

	}

	public Score getScoreById(Integer id) {
		return scoreMapper.getScoreById( id );
	}

	public Integer updScore(Integer id, Integer result) {
		Map<String,Integer> map = new HashMap<String, Integer>(  );
		map.put( "id",id );
		map.put( "result",result );

		return scoreMapper.updScore( map );
	}

}
